#include "VideoViewFrame.h"
#include<QGridLayout>





VideoViewFrame::VideoViewFrame(QWidget *parent, int row, int col)
	: QFrame(parent)
{
	gridLay = new QGridLayout(this);
	gridLay->setSpacing(2);
	for (int i = 0; i < row; i++) {

		for(int j = 0; j < col; j++){
			QFrame* child = new QFrame(this);
			//child->setStyleSheet(".QFrame {background-color:#9E9E9E;}");
			flagMap.insert(child, false);
			gridLay->addWidget(child, i, j);
			child->setToolTip(QString::number(i) + QString("-") + QString::number(j));
		}
	}
}

VideoViewFrame::~VideoViewFrame()
{

}

QFrame* VideoViewFrame::getUnusedFrame()
{
	for (int i = 0; i < gridLay->rowCount(); ++i) {
		for (int j = 0; j < gridLay->columnCount(); ++j) {
			QFrame* child = dynamic_cast<QFrame*>(gridLay->itemAtPosition(i, j)->widget());
			if (flagMap.value(child) == false  && child->isVisible()) {
				return child;
			}
		}
	}
	return nullptr;
}

void VideoViewFrame::setUsed(QFrame* frame,bool isUse)
{
	for (QMap<QFrame*, bool>::iterator iter = flagMap.begin(); iter != flagMap.end(); iter++) {
		if (iter.key() == frame) {
			flagMap[frame] = isUse;
		}
	}
}

void VideoViewFrame::setGridShow(int row,int col)
{
	for (int i = 0; i < gridLay->rowCount(); ++i) {
		for (int j = 0; j < gridLay->columnCount(); ++j) {

			if (i < row && j <col) {
				gridLay->itemAtPosition(i, j)->widget()->setVisible(true);
			}
			else {
				gridLay->itemAtPosition(i, j)->widget()->setVisible(false);
			}
		
		}	
	}
}


void VideoViewFrame::setUnuseAll()
{
	for (QMap<QFrame*, bool>::iterator iter = flagMap.begin(); iter != flagMap.end(); iter++) {
		flagMap[iter.key()] = false;
	}
}

QFrame* VideoViewFrame::getFrame(int row, int col)
{
	if (row < gridLay->rowCount() && col < gridLay->columnCount()){
		return dynamic_cast<QFrame*>(gridLay->itemAtPosition(row, col)->widget());
	}else{
		return nullptr;
	}
}

